﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq.SqlClient;

namespace VietImage.Models
{
    public partial class Customer
    {
        #region Properties
        public List<DetailCustomer> Details
        {
            get
            {
                return this.GetDetails();
            }
        }

        public User User
        {
            get
            {
                return this.GetUser();
            }
        }

        public Status Status
        {
            get
            {
                return this.GetStatus();
            }
        }
        #endregion

        #region  Methods
        public DetailCustomer GetDetail(int lang)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.DetailCustomers.Where(p => p.ID_Language == lang && p.ID_Customer == this.ID);
            if (v.Count() == 0)
                return null;
            return v.First();
        }

        public List<DetailCustomer> GetDetails()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.DetailCustomers.Where(p => p.ID_Customer == this.ID);
            if (v.Count() == 0)
                return null;
            return v.ToList();
        }

        public User GetUser()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Users.Where(p => p.ID == this.ID_User);
            if (v.Count() == 0)
                return null;
            return v.First();
        }

        public Status GetStatus()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Status.Where(p => p.ID == this.ID_Status);
            if (v.Count() == 0)
                return null;
            return v.First();
        }
        #endregion

        #region Static Methods
        public static Customer Parse(System.Object value)
        {
            Customer temp = null;
            try
            {
                temp = (Customer)value;
            }
            catch (Exception ex)
            {
            }
            return temp;
        }

        public static List<Customer> GetList()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Customers;
            if (v.Count() == 0)
                return null;
            return v.ToList();
        }

        public static List<Customer> GetListAvailable()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Customers.Where(p => p.ID_Status != 2);
            if (v.Count() == 0)
                return null;
            return v.ToList();
        }

        public static List<Customer> GetListNotAvailable()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Customers.Where(p => p.ID_Status == 2);
            if (v.Count() == 0)
                return null;
            return v.ToList();
        }

        private static List<ErrorItem> InputInfomationValidation(Customer customer)
        {
            List<ErrorItem> errorList = new List<ErrorItem>();
            if (customer.ID_Status < 1)
                errorList.Add(new ErrorItem("ID_Status", "ID_Status is required."));
            if (customer.ID_User < 1)
                errorList.Add(new ErrorItem("ID_User", "ID_User is required."));
            return errorList;
        }

        public static List<ErrorItem> Insert(ref Customer ii, ref DetailCustomer di, ref User ui, int id_User, int id_Lang)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            List<ErrorItem> errorList = InputInfomationValidation(ii);
            if (errorList.Count == 0)
            {
                Nullable<Int32> id = null;
                int errorMess = _db.InsertCustomer(ref id, id_User, ii.Price);
                if (errorMess == 1)
                {
                    ii.ID = id.Value;
                }
                
                if(errorMess != 1) errorList.Add(new ErrorItem("ErrorMessage", _db.ErrorMessages.Single(p => p.ID_ErrorMessage == errorMess && p.ID_Language == id_Lang).Description));
            }
            return errorList;
        }

        public static List<ErrorItem> ChangeInformation(Customer ic, int id_User, int id_Lang)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            List<ErrorItem> errorList = InputInfomationValidation(ic);
            if (errorList.Count == 0)
            {
                Customer oi = _db.Customers.Single(p => p.ID == ic.ID);
                String content = "";
                content = oi.ID_User + "_" + oi.Price + "_" + oi.ID_Status;
                int errorMess = _db.ChangeCustomer(ic.ID, id_User, ic.Price, ic.ID_Status, content, 0, 0);
                
                    if(errorMess != 1) errorList.Add(new ErrorItem("ErrorMessage", _db.ErrorMessages.Single(p => p.ID_ErrorMessage == errorMess && p.ID_Language == id_Lang).Description));
            }
            return errorList;
        }

        public static List<ErrorItem> ChangeInformation(Customer ic, int id_User, int id_Log, int id_Lang)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            List<ErrorItem> errorList = InputInfomationValidation(ic);
            if (errorList.Count == 0)
            {
                Customer oi = _db.Customers.Single(p => p.ID == ic.ID);
                int errorMess = _db.ChangeCustomer(ic.ID, id_User, ic.Price, ic.ID_Status, "", id_Log, 1);

                if(errorMess != 1) errorList.Add(new ErrorItem("ErrorMessage", _db.ErrorMessages.Single(p => p.ID_ErrorMessage == errorMess && p.ID_Language == id_Lang).Description));
            }
            return errorList;
        }

        public static List<ErrorItem> Delete(int id_Customer, int id_User, int deep, int id_Lang)
        {
            List<ErrorItem> errorList = new List<ErrorItem>();
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();

            int errorMess = _db.DeleteCustomer(id_User, id_Customer, deep);
            
                if(errorMess != 1) errorList.Add(new ErrorItem("ErrorMessage", _db.ErrorMessages.Single(p => p.ID_ErrorMessage == errorMess && p.ID_Language == id_Lang).Description));


            return errorList;
        }
        #endregion

        #region Search
        public static List<Customer> Search(String name, String address)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = from du in _db.DetailUsers
                       join c in _db.Customers on du.ID_User equals c.ID
                       where SqlMethods.Like(du.Firstname, "%" + name + "%") || SqlMethods.Like(du.Lastname, "%" + name + "%") || SqlMethods.Like(du.Address, "%" + address + "%")
                       select c;

            if (v.Count() == 0)
                return null;
            return v.OrderByDescending(p=>p.ID).ToList();
        }

        private static String BuildQuery(Dictionary<String, System.Object> parametersList, Dictionary<String, bool> exactList)
        {
            String query = "";

            DateTime noValue = new DateTime(2000, 1, 1);

            foreach (KeyValuePair<String, System.Object> kvp in parametersList)
            {
                if (query.Length != 0)
                    query += " AND ";
                if (kvp.Key == "ID" || kvp.Key == "ID_Status" || kvp.Key == "ID_Language" || kvp.Key == "ID_Status" || kvp.Key == "ID_User")
                {
                    if (int.Parse(kvp.Value.ToString()) >= 1)
                        query += kvp.Key + " = " + int.Parse(kvp.Value.ToString());
                }
                if (kvp.Key == "Price")
                {
                    if (int.Parse(kvp.Value.ToString()) >= 0)
                        query += kvp.Key + " = " + int.Parse(kvp.Value.ToString());
                }
                else if (kvp.Key == "CreatedTime")
                {
                    if (DateTime.Parse(kvp.Value.ToString()) != noValue)
                        query += "CreatedTime" + " = " + DateTime.Parse(kvp.Value.ToString()).ToString("yyyy-MM-dd") + "";
                }
            }

            if (parametersList.ContainsKey("CreatedbeginDate") && parametersList.ContainsKey("CreatedendDate"))
            {
                query += " DATEDIFF(day, " + DateTime.Parse(parametersList["CreatedendDate"].ToString()).ToString("yyyy-MM-dd") + " , CreatedTime) >= 0";
                query += " DATEDIFF(day, CreatedTime , " + DateTime.Parse(parametersList["CreatedbeginDate"].ToString()).ToString("yyyy-MM-dd") + ") >= 0";
            }

            return query;
        }

        public static System.Object GetCustomer(Dictionary<String, System.Object> parametersList, Dictionary<String, bool> exactList, bool oneRow)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            String query = BuildQuery(parametersList, exactList);
            var v = _db.ExecuteQuery<Customer>(query);
            if (oneRow)
            {
                if (v.Count() == 0)
                    return null;
                return v.First();
            }
            if (v.Count() == 0)
                return null;
            return v.OrderByDescending(p => p.ID).ToList();
        }

        #endregion
    }
}